<?php
Class admin_commentController Extends baseController {

	public function index() { 
		$keyword = (isset($_REQUEST['keyword'])) ? $_REQUEST['keyword'] : '';
		$where = '1=1';
		if($keyword != ''){
			$where .= ' and (noidung like "%'.$keyword.'%" or tendangnhap like "%'.$keyword.'%" or tivi_id in (select id from tbltivi where TenTivi like "%'.$keyword.'%"))';
		}
		$commentModel = new comment();
		$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
		$pos = ($current_page -1 ) * __PAGE_SIZE;
		$lstTv = $commentModel->getCommentWherePaging($where, 'thoigian desc',$pos);
		$count = count((array)$commentModel->getCommentByWhere($where, ''));
		$pages = ceil($count/__PAGE_SIZE);

		$this->registry->template->lstComment = $lstTv;
		$this->registry->template->pages = $pages;
		
		$this->registry->template->show('admin/comment_view');
	}
	
	public function edit(){
		if(isset($_REQUEST['comment_id'])){
			$comment_id = $_REQUEST['comment_id'];
			$commentModel = new comment();
			$comment = $commentModel->getCommentById($comment_id);
			if(count($comment)>0){
				$tiviModel = new tivi();
				$lsttv = $tiviModel->getTiviByWhere('t.TrangThai=1','TenTivi');
				$this->registry->template->comment = $comment;	
				$this->registry->template->lsttv = $lsttv;	
			}
		}
		$this->registry->template->show('admin/comment_edit');	
	}
	
	public function save(){
			
			$comment = new comment();
			$comment->tendangnhap = trim($_POST['ttendangnhap']);
			$comment->tivi_id = trim($_POST['ttivi_id']);
			$comment->parent_id = trim($_POST['tparent_id']);
			$comment->noidung = trim($_POST['tnoidung']);
			$comment->thoigian = date('Y-m-d G:i:s',strtotime(trim($_POST['tthoigian'])));
			
			$comment->trangthai = $_POST['rtrangthai'];
			
			
			if(isset($_REQUEST['comment_id'])){
				$comment->id = $_GET['comment_id'];
				$rs = $comment->update($comment);
				
			}
			else{
				
				$rs = $comment->insert($comment);
			}
			
			if($rs){
				echo '<script>alert("thành công.");</script>';
				header('location:'.__URL_BASE.'index.php?rt=admin_comment');
				exit;
			}
			else{
				echo '<script>alert("Có lỗi xảy ra.");</script>';
			}
		}
		
		public function delete(){
			if(isset($_REQUEST['comment_id'])){
				$comment = new comment();
				$comment->delete($_REQUEST['comment_id']);
				header('location:'.__URL_BASE.'index.php?rt=admin_comment');
				exit;
			}
		}

}
?>
